package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.DTO.property.UserDeviceAuthAddreddDTO;
import com.itlong.cloud.POJO.DTO.property.UserDeviceAuthAddressDTO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.property.service.IPropertyDeviceAuthService;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <desc>
 *      住户设备权限_根据地址删除住户其他类别的设备权限
 * </desc>
 *
 * @createDate 2017-11-08
 */
@Component
public class HouseHoldRemoveAuthConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(HouseHoldRemoveAuthConsumer.class);

    @Autowired
    IPropertyDeviceAuthService iPropertyDeviceAuthService;

    /**
     * <desc>
     *      用户设备权限业务操作队列消费者
     * </desc>
     * @param message 消息
     * @param channel
     * @return
     * @author zhangs
     * @createDate 2017/11/6
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_USER_REMOVE_DEVICE_AUTH_BY_ADDR_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException, IOException{
        byte[] body = message.getBody();
        //LOG.info("RabbitMQ_用户设备权限_根据地址删除住户其他类别的设备权限业务消费端:"+body);
        try {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JSON.parseObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            Map<String, Object> dataMap = JsonUtil.toObject(timeTaskDeadMessageQueuePO.getMessageContent(), HashMap.class);
            String data = dataMap.get("data").toString();
            UserDeviceAuthAddressDTO userDeviceAuthAddressDTO = JSON.parseObject(data, UserDeviceAuthAddressDTO.class);
            List<UserDeviceAuthAddreddDTO> userDeviceAuthAddreddDTOS = userDeviceAuthAddressDTO.getUserDeviceAuthAddreddDTOS();
            if(userDeviceAuthAddreddDTOS != null && !userDeviceAuthAddreddDTOS.isEmpty()) {
                for (UserDeviceAuthAddreddDTO userDeviceAuthAddreddDTO:userDeviceAuthAddreddDTOS) {
                    Map<String, Object> userAuthRoomAddrMap = userDeviceAuthAddreddDTO.getUserAuthRoomAddrMap();
                    Set<String> buildIdSet = userAuthRoomAddrMap.keySet();
                    for (String buildId : buildIdSet) {
                        Map<String, String> unitMap = JSON.parseObject(JSON.toJSONString(userAuthRoomAddrMap.get(buildId)), HashMap.class);
                        Set<String> unitIsSet = unitMap.keySet();
                        for (String unitId:unitIsSet) {
                            String roomNum = JSON.parseObject(JSON.toJSONString(unitMap.get(unitId)), String.class);
                            unitMap.put(unitId, roomNum);
                        }
                        userAuthRoomAddrMap.put(buildId, unitMap);
                    }
                }
                iPropertyDeviceAuthService.delUserOtherAuth(userDeviceAuthAddreddDTOS);
            }

        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            LOG.error("【RabbitMQ_用户设备权限_根据地址删除住户其他类别的设备权限业务消费端】:解析异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
